# Data wrangling
library(tidyverse)
# Plotting
library(cowplot)
library(plotly)
library(factoextra)
source('/home/jin/OneDrive/SGB/projects/functions.R')

Project description

Study summary

This is where I would write a summary about the study/project - i.e. how it came about, who is involved, research question, what types of data were delivered, etc.

Objectives

  • List research objectives here
  • You can use asterisk or dash to make a list

Data preparation

Load data

For the demo, we’ll just use the mtcars dataset.

mtcars

Plots and Figures

Output from plotting functions are included.

mtcars %>%
  select(mpg, cyl, disp, hp) %>%
  rownames_to_column(var='Model') %>%
  pivot_longer(cols=-Model) %>%
  ggplot(mapping=aes(x=Model,value)) +
  geom_bar(stat='identity') +
  facet_grid(name ~ ., scales='free') + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

2D and 3D PCA plots

For .html output, you can include 3D interactive plots using plotly package.

2D PCA

res.pca <- prcomp(mtcars)
fviz_pca_ind(res.pca,
             col.ind=as.factor(mtcars$cyl),
             addEllipses = TRUE,
             legend.title='cyl',
             repel=TRUE,
             title='')

3D PCA

plotly_pca(res.pca, as.factor(mtcars$cyl))

Appendix

Code

.tocify-header {
  text-indent: initial;
}

.tocify-subheader > .tocify-item {
  text-indent: initial;
  padding-left: 20px;
}

.tocify-subheader .tocify-subheader > .tocify-item {
  text-indent: initial;
  padding-left: 30px;
}

.tocify-subheader .tocify-subheader .tocify-subheader > .tocify-item {
  text-indent: initial;
  padding-left: 40px;
}

.tocify-subheader .tocify-subheader .tocify-subheader > .tocify-item {
  text-indent: initial;
  padding-left: 50px;
}
knitr::opts_chunk$set(
  echo = TRUE,
  include = TRUE,
  cache = FALSE,
  dpi = 150,
  cache.lazy = FALSE
  )
# Data wrangling
library(tidyverse)
# Plotting
library(cowplot)
library(plotly)
library(factoextra)
source('/home/jin/OneDrive/SGB/projects/functions.R')
mtcars
mtcars %>%
  select(mpg, cyl, disp, hp) %>%
  rownames_to_column(var='Model') %>%
  pivot_longer(cols=-Model) %>%
  ggplot(mapping=aes(x=Model,value)) +
  geom_bar(stat='identity') +
  facet_grid(name ~ ., scales='free') + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))
res.pca <- prcomp(mtcars)
fviz_pca_ind(res.pca,
             col.ind=as.factor(mtcars$cyl),
             addEllipses = TRUE,
             legend.title='cyl',
             repel=TRUE,
             title='')
plotly_pca(res.pca, as.factor(mtcars$cyl))

sessionInfo()

R session info

sessionInfo()
## R version 4.1.3 (2022-03-10)
## Platform: x86_64-redhat-linux-gnu (64-bit)
## Running under: CentOS Stream 9
## 
## Matrix products: default
## BLAS/LAPACK: /usr/lib64/libopenblas-r0.3.15.so
## 
## locale:
##  [1] LC_CTYPE=en_CA.UTF-8       LC_NUMERIC=C               LC_TIME=en_CA.UTF-8       
##  [4] LC_COLLATE=en_CA.UTF-8     LC_MONETARY=en_CA.UTF-8    LC_MESSAGES=en_CA.UTF-8   
##  [7] LC_PAPER=en_CA.UTF-8       LC_NAME=C                  LC_ADDRESS=C              
## [10] LC_TELEPHONE=C             LC_MEASUREMENT=en_CA.UTF-8 LC_IDENTIFICATION=C       
## 
## attached base packages:
## [1] stats     graphics  grDevices utils     datasets  methods   base     
## 
## other attached packages:
##  [1] officer_0.4.3    officedown_0.2.4 factoextra_1.0.7 plotly_4.10.0    cowplot_1.1.1   
##  [6] forcats_0.5.1    stringr_1.4.0    dplyr_1.0.9      purrr_0.3.4      readr_2.1.2     
## [11] tidyr_1.2.0      tibble_3.1.8     ggplot2_3.3.6    tidyverse_1.3.2  DT_0.23         
## 
## loaded via a namespace (and not attached):
##  [1] fs_1.5.2            lubridate_1.8.0     httr_1.4.3          rprojroot_2.0.3    
##  [5] tools_4.1.3         backports_1.4.1     bslib_0.4.0         utf8_1.2.2         
##  [9] R6_2.5.1            lazyeval_0.2.2      DBI_1.1.3           colorspace_2.0-3   
## [13] withr_2.5.0         tidyselect_1.1.2    prettyunits_1.1.1   processx_3.7.0     
## [17] curl_4.3.2          compiler_4.1.3      cli_3.3.0           rvest_1.0.2        
## [21] xml2_1.3.3          labeling_0.4.2      sass_0.4.2          scales_1.2.0       
## [25] callr_3.7.1         systemfonts_1.0.4   digest_0.6.29       rmarkdown_2.14     
## [29] pkgconfig_2.0.3     htmltools_0.5.3     dbplyr_2.2.1        fastmap_1.1.0      
## [33] highr_0.9           rvg_0.2.5           htmlwidgets_1.5.4   rlang_1.0.4        
## [37] readxl_1.4.0        rstudioapi_0.13     xaringan_0.27       farver_2.1.1       
## [41] jquerylib_0.1.4     generics_0.1.3      jsonlite_1.8.0      crosstalk_1.2.0    
## [45] zip_2.2.0           car_3.1-0           googlesheets4_1.0.0 magrittr_2.0.3     
## [49] Rcpp_1.0.9          munsell_0.5.0       fansi_1.0.3         gdtools_0.2.4      
## [53] abind_1.4-5         lifecycle_1.0.1     stringi_1.7.8       yaml_2.3.5         
## [57] carData_3.0-5       pkgbuild_1.3.1      grid_4.1.3          ggrepel_0.9.1      
## [61] promises_1.2.0.1    crayon_1.5.1        haven_2.5.0         hms_1.1.1          
## [65] knitr_1.39          ps_1.7.1            pillar_1.8.0        uuid_1.1-0         
## [69] ggpubr_0.4.0        ggsignif_0.6.3      reprex_2.0.1        servr_0.25         
## [73] glue_1.6.2          evaluate_0.15       data.table_1.14.2   remotes_2.4.2      
## [77] modelr_0.1.8        vctrs_0.4.1         tzdb_0.3.0          httpuv_1.6.5       
## [81] cellranger_1.1.0    gtable_0.3.0        assertthat_0.2.1    cachem_1.0.6       
## [85] xfun_0.31           mime_0.12           broom_1.0.0         rstatix_0.7.0      
## [89] later_1.3.0         viridisLite_0.4.0   googledrive_2.0.0   gargle_1.2.0       
## [93] memoise_2.0.1       ellipsis_0.3.2